package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     */
    //http://localhost:8080/shoppingCart/add  Request Method: POST
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart ) {
        log.info("shoppingCart:{}", shoppingCart);

        ShoppingCart shoppingCart1 = shoppingCartService.add(shoppingCart);

        return R.success(shoppingCart1);


    }

    /**
     * 购物车 减
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
       /*
            业务处理逻辑：
            传过来一个id，首先判断是菜品还是套餐
            然后获取当前用户购物车信息
            找到对应的 菜品或者套餐  获取number
            操作
        */

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else{
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();

        if (number>1){
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }else if (number == 1){
            one.setNumber(number-1);
            shoppingCartService.removeById(one);
        }
        return R.success(one);
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("进到查询购物车");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * 清空购物车
     * Request URL: http://localhost:8080/shoppingCart/clean
     * Request Method: DELETE
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //Request URL: http://localhost:8080/shoppingCart/clean
        //Request Method: DELETE

        shoppingCartService.clean();
    /*    //用户id
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //清空
        shoppingCartService.remove(lqw);*/

        return R.success("清空购物车成功");
    }

}
